/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.server.impl;


import me.zhengjie.dao.WareTypeDao;

import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import me.zhengjie.dao.dto.WareTypeQueryCriteria;
import me.zhengjie.mapper.WareTypeMapper;
import me.zhengjie.server.WareTypeService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;


/**
* @description 服务实现
* @author laoxu
* @date 2025-06-18
**/
@Service
@RequiredArgsConstructor
public class WareTypeServiceImpl extends ServiceImpl<WareTypeMapper, WareTypeDao> implements WareTypeService {

    private final WareTypeMapper wareTypeMapper;
    @Override
    public PageResult<WareTypeDao> queryAll(WareTypeQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(wareTypeMapper.findAll(criteria, page));
    }

    @Override
    public List<WareTypeDao> queryAll(WareTypeQueryCriteria criteria){
        return wareTypeMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(WareTypeDao resources) {
        resources.setCreateDate(new Timestamp(new Date().getTime()));
        wareTypeMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WareTypeDao resources) {
        WareTypeDao wareType = getById(resources.getId());
        resources.setUpdateDate(new Timestamp(new Date().getTime()));
        wareType.copy(resources);
        wareTypeMapper.updateById(wareType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) {
        wareTypeMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<WareTypeDao> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WareTypeDao wareType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("库区类型", wareType.getType());
            map.put("创建时间", wareType.getCreateDate());
            map.put("更新时间", wareType.getUpdateDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}